package com.glorial.util;

import java.util.Date;
import java.util.UUID;

public class CommonUtil {
	public static String getUuid(){
		return (UUID.randomUUID().toString()).replace("-", "");
	}
	
    public static byte[] getUTF8(String s) throws Exception{ 
    	return s.getBytes("utf-8");
    }
    
    public static byte[] getUTF8(Object s) throws Exception{
    	if(s instanceof Date){
    		return getUTF8(StringUtil.format("yyyyMMddhhmmss", (Date)s));
    	}else if(s instanceof Integer){
    		return getUTF8(((Integer)s).toString());
    	}else{
    		return getUTF8(s.toString());
    	}
    }
    
    public static String getString(Object s) throws Exception{
    	if(s instanceof Date){
    		return getString(StringUtil.format("yyyyMMddhhmmss", (Date)s));
    	}else if(s instanceof Integer){
    		return getString(((Integer)s).toString());
    	}else{
    		return getString(((String)s).getBytes());
    	}
    }
    
    public static String getString(byte[] b) throws Exception{
		return new String(b, "utf-8");
    }
    
    public static Object getObject(Class cls, byte[] val) throws Exception{
    	if(cls == Date.class){
    		return StringUtil.parseDate(CommonUtil.getString(val));
		}else if(cls == Integer.class){
			return new Integer(CommonUtil.getString(val));
		}else{
			return CommonUtil.getString(val);
		}
    }
}
